package org.zhangsh.deploy;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.persistence.entity.DeploymentEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zhangsh.common.IpUtil;

import java.util.List;

/**
 * </p>
 *
 * @author zhangsh
 * @version 1.0.0
 * @since 2019-01-04
 */
@RestController
public class DeployController {

    @Autowired
    private SpringProcessEngineConfiguration processEngineConfiguration;

    /**
     * 流程文件部署及流程定义业务组件
     */
    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/deploy")
    public String deploy(){

        DeploymentEntity deploymentEntity = (DeploymentEntity)repositoryService.createDeployment().
                addClasspathResource("bpmns/jtAudit.bpmn").name(processEngineConfiguration.getDeploymentName())
                .category("category")
                .tenantId(IpUtil.getLocalIp())
                .deploy();

        List<ProcessDefinitionEntity> list = deploymentEntity.getDeployedArtifacts(ProcessDefinitionEntity.class);

        for(ProcessDefinitionEntity e : list){

            System.out.println("+++++++++++++++++++++++++++++++++");

            System.out.println("部署ID：" + e.getDeploymentId());
            System.out.println("流程定义ID：" + e.getId());
            System.out.println("流程定义KEY：" + e.getKey());
            System.out.println("流程名称：" + e.getName());
            System.out.println("流程图名："+ e.getDiagramResourceName());
        }

        return "success";
    }

}
